<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
* Description of IndexController
*
*/

class Admin_HomepageController extends Zend_Controller_Action
{

	public function init(){
		 
	}

    public function indexAction()
    {
		Zend_Loader::loadFile("../application/admin/models/Homepage.php");
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		if(!$data){
		    $this->_redirect('admin/auth/login');
		}
		
		$this->_helper->layout->setLayout('admin_layout');
		$request = $this->getRequest();
		$Homepage = new Homepage();
		$homepagearr=$Homepage->listhomepagecontent();
		$stat = $request->getParam('stat');
		//  print_r($Homarr);exit;
		$this->view->stat = $stat;
		$this->view->homepagearr=$homepagearr;
	}
    
	public function addAction()
    {
        Zend_Loader::loadFile('../application/admin/forms/Homepage.php');
       $this->_helper->layout->setLayout('admin_layout');
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $homepage_form = new Form_homepage($options);
        $this->view->homepage_form = $homepage_form; 
        $this->view->title = "Edit";

    }
    
    public function editAction()
    {
		Zend_Loader::loadFile("../application/admin/models/Homepage.php");
		Zend_Loader::loadFile("../application/admin/forms/Homepage.php");
		$homepagemodel=new homepage();
		$this->_helper->layout->setLayout('admin_layout');
		$controller = $this->getRequest()->getControllerName();
		$action = $this->getRequest()->getActionName();
		$options = array("action"=>$action);
		$id = $this->_getParam('id', 0);		
		
		$subsect_arr = $homepagemodel->getsubsectionshomepage($id);
		
		$options = array("action"=>$action, "id"=>$id, "subsect_arr"=>$subsect_arr);
		
		$homepageform = new Form_homepage($options);
		$this->view->homepageform =  $homepageform;
		$this->view->id = $id;
		if ($id > 0) {
			$homepageform->populate($homepagemodel->gethomepagebyid($id));
		}      
		//$homepagedata=$homepagemodel->gethomepage($id);
	}

	public function doaddAction()
    {
		Zend_Loader::loadFile("../application/admin/models/Homepage.php");
		Zend_Loader::loadFile("../application/admin/forms/Homepage.php");
		// print_r($_POST);
		$request = $this->getRequest();
		$homepage = new Homepage();
		$controller = $this->getRequest()->getControllerName();
		$action = $this->getRequest()->getActionName();
		$options = array("action"=>$action);
		$homepageform = new Form_homepage($options);
		
		
		
		if($this->getRequest()->isPost()){
			$formdata = $this->getRequest()->getPost();
			
			if($homepageform->isValid($formdata)){
				$insertarr["title"]= preg_replace ( "/'/", "\\'", $request->getParam('title') );
				$insertarr["content"]=preg_replace ( "/'/", "\\'", $request->getParam('content') );
				$insertarr["section"]=preg_replace ( "/'/", "\\'",$request->getParam('section') );
				$insertarr["sub_section"]=preg_replace ( "/'/", "\\'",$request->getParam('sub_section') );
				$insertarr["isactive"]=preg_replace ( "/'/", "\\'",$request->getParam('isactive') );
                $addhomepage=$homepage->addhomepagecontent($insertarr)  ;
                if($addhomepage)
                {
                	$this->_redirect('/admin/homepage/index/stat/updated');
                }
			}
            else{
				$homepageform->populate($formdata);
				$this->_helper->layout->setLayout('admin_layout');
				$this->view->homepage_form = $homepageform;
			}
		}   
    }

    public function doeditAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Homepage.php");
        Zend_Loader::loadFile("../application/admin/forms/Homepage.php");
      // print_r($_POST);
        $modhomepage=new homepage();
        
        $request = $this->getRequest();
        $controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $homepageform=new Form_homepage($options);
        
         if($this->getRequest()->isPost()){
            $formdata = $this->getRequest()->getPost();
            if($homepageform->isValid($formdata)){
                 $insertarr["title"]= preg_replace ( "/'/", "\\'", $request->getParam('title') );
                    $insertarr["content"]=preg_replace ( "/'/", "\\'", $request->getParam('content') );
                     $insertarr["section"]=preg_replace ( "/'/", "\\'",$request->getParam('section') );
                     $insertarr["sub_section"]=preg_replace ( "/'/", "\\'",$request->getParam('sub_section') );
                    $insertarr["isactive"]=preg_replace ( "/'/", "\\'",$request->getParam('isactive') );
                    $insertarr["id"]=preg_replace ( "/'/", "\\'",$request->getParam('id') );
                $edithomepage=$modhomepage->edithomepage($insertarr)  ;
                if($edithomepage)
                {
                    $this->_redirect('admin/homepage/index/stat/updated');
                }
            }
            else{
                $homepageform->populate($formdata);
                $this->_helper->layout->setLayout('admin_layout');
                $this->view->homepageform = $homepageform;
            }

            
         }
        
    }

     public function deleteAction()
    {
        Zend_Loader::loadFile("../application/admin/models/Homepage.php");
      // print_r($_POST);
        $modhomepage=new homepage();
        $request = $this->getRequest();
        $id=$request->getParam('id');
        $deletehomepage=$modhomepage->deletehomepage($id);
        if($deletehomepage)
        {
             $this->_redirect('admin/homepage/index/stat/deleted');
        }
     }
     public function changehomepagestatusAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/Homepage.php");
		$modhomepage = new Homepage();
		$request = $this->getRequest();
		$id = $request->getParam('id');
		$modhomepage->changehomepagestatus($id);
	}
	
	public function addtabcontentAction()
	{
		$this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/forms/Homepage.php");
		
		$controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
        $homepage_form = new Form_homepage($options);
		$this->view->title = "Add Tabcontent";		
        $this->view->homepage_form = $homepage_form;
        $this->view->homepage_form = $homepage_form; 
	}
	
	public function doaddtabcontentAction()
	{
		$this->_helper->layout->setLayout('admin_layout');
		Zend_Loader::loadFile("../application/admin/forms/Homepage.php");
		Zend_Loader::loadFile("../application/admin/models/Homepage.php");
		$request = $this->getRequest();
		$controller = $this->getRequest()->getControllerName();
        $action = $this->getRequest()->getActionName();
        $options = array("action"=>$action);
		$homepage_form = new Form_homepage($options);
		if ($this->getRequest()->isPost()) { 
			$formdata = $request->getPost();
			if($homepage_form->isValid($formdata)){           
				$modHomepage = new Homepage();
				$addtabdata = $modHomepage->addTabData($formdata);
				$this->_redirect('/admin/homepage/index/stat/added');			
			} else {
               $homepage_form->populate($formdata);               
               $this->view->homepage_form = $homepage_form;
              }
		}
	}
	
	public function getsubsectionAction()
	{
		$this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile("../application/admin/models/Homepage.php");
		$modHomepage = new Homepage();
		$request = $this->getRequest();		
		$sectid = $request->getParam('sectid');
		$subsect_arr = $modHomepage->gethomepagesubsections($sectid);
		$return_str = "<option value=''>select sub section</option>";
		foreach ($subsect_arr as $subsect_entry)
		{
			$return_str .= "<option  label='" . $subsect_entry['title'] . "' value=" . $subsect_entry['id'] . ">" . $subsect_entry['title'] . "</option>";
		}
		
		echo $return_str;
	}
}
?>
